package net.keebook.webapp.bean;

import java.io.IOException;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.AccountConfirmationDao;
import net.keebook.webapp.dao.SettingDao;
import net.keebook.webapp.dao.UserDao;
import net.keebook.webapp.entity.User;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.AccountConfirmationMail;

@ManagedBean
@RequestScoped
public class CompleteProfileBean {
	private String name;
	private String email;
	private Boolean termsAccepted;
	private SessionModel sessionModel;
	@Inject
	private UserDao userDao;
	@Inject
	private LoginSupportBean loginSupportBean;
	@Inject
	private AccountConfirmationDao accountConfirmationDao;
	@Inject
	private SettingDao settingDao;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
		
		User user = getUserDao().get(getSessionModel().getUserId());
		setName(user.getName());
		setEmail(user.getEmail());
		setTermsAccepted(user.getTermsAccepted());
	}
	
	public void apply() throws IOException {
		User user = getUserDao().get(getSessionModel().getUserId());
		boolean requireAccountConfirmation = false;
		
		if (user.getEmail() == null || !user.getEmail().equals(getEmail())) {
			// Email changed, require account confirmation
			requireAccountConfirmation = true;
		}
		
		user.setName(getName());
		user.setEmail(getEmail());
		user.setTermsAccepted(getTermsAccepted());
		getUserDao().update(user);
		
		getSessionModel().setRequireProfileCompletion(false);
		
		if (requireAccountConfirmation) {
			AccountConfirmationMail.send(user, getAccountConfirmationDao(), getSettingDao());
		}
		
		FacesContext.getCurrentInstance().getExternalContext().redirect(getLoginSupportBean().getRedirectionUrl(getSessionModel()));
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Boolean getTermsAccepted() {
		return termsAccepted;
	}
	public void setTermsAccepted(Boolean termsAccepted) {
		this.termsAccepted = termsAccepted;
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public LoginSupportBean getLoginSupportBean() {
		return loginSupportBean;
	}

	public void setLoginSupportBean(LoginSupportBean loginSupportBean) {
		this.loginSupportBean = loginSupportBean;
	}

	public AccountConfirmationDao getAccountConfirmationDao() {
		return accountConfirmationDao;
	}

	public void setAccountConfirmationDao(AccountConfirmationDao accountConfirmationDao) {
		this.accountConfirmationDao = accountConfirmationDao;
	}

	public SettingDao getSettingDao() {
		return settingDao;
	}

	public void setSettingDao(SettingDao settingDao) {
		this.settingDao = settingDao;
	}
}
